package com.wjk.kylin.mall.product.config;

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author admin
 * @description es 配置类
 * @date 2021/6/30
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticsearchConfig {

    private String host;
    private int port;
//    private String username;
//    private String password;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
//        /*用户认证对象*/
//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        /*设置账号密码*/
////        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
//        /*RestHighLevelClient*/
//        return new RestHighLevelClient(RestClient.builder(new HttpHost(host, port))
//                .setHttpClientConfigCallback(httpAsyncClientBuilder -> {
//                    httpAsyncClientBuilder.disableAuthCaching();
//                    return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//                }));

        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(host, port))
        );
    }
}
